#!/usr/bin/perl -w

##HACKABOT_HELP##
# List and get help for the available commands
# !help [command name]
##HACKABOT_HELP##

use strict;

use XML::Simple;
use IO::Handle;
STDOUT->autoflush(1);

my $MODS = $ENV{'HACKABOT_CMD'};
if (not defined $MODS) {
	die "help: Uh, are you running this from hackabot?";
}

my $cfgfile = shift;
if (not defined $cfgfile) {
	if (not defined $ENV{'HACKABOT_ETC'}) {
		die "Error HACKABOT_ETC is undefined";
	}
	$cfgfile = "$ENV{'HACKABOT_ETC'}/acl.xml";
}

our $ACCESS = XMLin($cfgfile,
	ForceArray => ['command', 'public', 'person'],
	KeyAttr => { command => "name", public => 'chan', person => 'nick' });

our %event;
while (<>) {
	if (/^type\s*(\S*)/) {
		$event{'type'} = $1;
	}
	elsif (/^to\s*(\S*)/) {
		$event{'to'} = $1;
	}
	elsif (/^nick\s*(\S*)/) {
		$event{'nick'} = $1;
	}
	elsif (/^msg\s*(\S+)/) {
		$event{'msg'} = $1;
	}
}

if (defined $event{'msg'}) {
	showhelp($event{'msg'});
}
else {
	showcmds();
}
	

sub showhelp {
	my $cmd = shift;
	if (not -r "$MODS/$cmd" or not -x "$MODS/$cmd"
	    or not getacl($cmd) ) {
		return;
	}
	open(CMD, "$MODS/$cmd");
	
	my $inhelp = 0;
	while (<CMD>) {
		if (/^##HACKABOT_HELP##/) {
			if ($inhelp) {
				last;
			}
			else {
				$inhelp = 1;
			}
		}
		elsif ($inhelp) {
			s/^#\s*//;
			print "send ".$_;
		}
	}
}

sub showcmds {
	my @commands;
	opendir(MODDIR, $MODS);
	while ($_ = readdir(MODDIR)) {
		if (/^[^\.]/ and -x "/$MODS/$_" and not -l "/$MODS/$_"
		    and getacl($_) eq 'allow') {
			push(@commands, $_);
		}
	}
	closedir(MODDIR);

	print "send Commands: " . join(" ", sort @commands) . "\n";
}

sub getacl {
	my $cmd = shift;

if ($event{'type'} eq 'pubmsg') {
	if (defined $ACCESS->{'command'}->{$cmd}) {
		if (defined $ACCESS->{'command'}->{$cmd}->{'public'}->{$event{'to'}}) {
			if (defined $ACCESS->{'command'}->{$cmd}->{'public'}->{$event{'to'}}->{'person'}->{$event{'nick'}}) {
				return $ACCESS->{'command'}->{$cmd}->{'public'}->{$event{'to'}}->{'person'}->{$event{'nick'}}->{'action'} ;
			}
			else {
				return $ACCESS->{'command'}->{$cmd}->{'public'}->{$event{'to'}}->{'action'};
			}
		}
		elsif (defined $ACCESS->{'command'}->{$cmd}->{'person'}->{$event{'nick'}}) {
			return $ACCESS->{'command'}->{$cmd}->{'person'}->{$event{'nick'}}->{'action'}; 
		}
		else {
			return $ACCESS->{'command'}->{$cmd}->{'action'};
		}
	}
	else {
		if (defined $ACCESS->{'default'}->{'public'}->{$event{'to'}}) {
			if (defined $ACCESS->{'default'}->{'public'}->{$event{'to'}}->{'person'}->{$event{'nick'}}) {
				return $ACCESS->{'default'}->{'public'}->{$event{'to'}}->{'person'}->{$event{'nick'}}->{'action'}; 
			}
			else {
				return $ACCESS->{'default'}->{'public'}->{$event{'to'}}->{'action'};
			}
		}
		elsif (defined $ACCESS->{'default'}->{'person'}->{$event{'nick'}}) {
			return $ACCESS->{'default'}->{'person'}->{$event{'nick'}}->{'action'};
		}
		else {
			return $ACCESS->{'default'}->{'action'};
		}
	}
}	
elsif ($event{'type'} eq 'privmsg') {
	if (defined $ACCESS->{'command'}->{$cmd}) {
		if (defined $ACCESS->{'command'}->{$cmd}->{'private'}) {
			if (defined $ACCESS->{'command'}->{$cmd}->{'private'}->{'person'}->{$event{'nick'}}) {
				return $ACCESS->{'command'}->{$cmd}->{'private'}->{'person'}->{$event{'nick'}}->{'action'};
			}
			else {
				return $ACCESS->{'command'}->{$cmd}->{'private'}->{'action'};
			}
		}
		elsif (defined $ACCESS->{'command'}->{$cmd}->{'person'}->{$event{'nick'}}) {
			return $ACCESS->{'command'}->{$cmd}->{'person'}->{$event{'nick'}}->{'action'};
		}
		else {
			return $ACCESS->{'command'}->{$cmd}->{'action'};
		}
	}
	else {
		if (defined $ACCESS->{'default'}->{'private'}->{$event{'to'}}) {
			if (defined $ACCESS->{'default'}->{'private'}->{$event{'to'}}->{'person'}->{$event{'nick'}}) {
				return $ACCESS->{'default'}->{'private'}->{$event{'to'}}->{'person'}->{$event{'nick'}}->{'action'};
			}
			else {
				return $ACCESS->{'default'}->{'private'}->{$event{'to'}}->{'action'};
			}
		}
		elsif (defined $ACCESS->{'default'}->{'person'}->{$event{'nick'}}) {
			return $ACCESS->{'default'}->{'person'}->{$event{'nick'}}->{'action'};
		}
		else {
			return $ACCESS->{'default'}->{'action'};
		}
	}
}	

}

